package pkg;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

public class ClassReflection {

	private Class<?> myClass;
	private Object myObject;
	private Method myMethod;

	private static String PATH = "cfg.";

	public void createObj(String name) {
		try {
			myClass = Class.forName(PATH + name);

			Constructor<?> constructor = myClass.getConstructor();
			myObject = constructor.newInstance();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void invokeMethod(String name, Object[] params) {
		Object result;
		try {
			if (params.length > 0) {
				Class<?>[] pa = new Class[params.length];
				
				for(int i=0; i<params.length; i++) {
					pa[i] = params[i].getClass().getClass();
				}
				
				myMethod = myClass.getMethod(name, pa);
				result = myMethod.invoke(myObject, params);
				
			} else {
				myMethod = myClass.getMethod(name);
				result = myMethod.invoke(myObject);
			}

			System.out.println(result);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}
